package finassist.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;



import finassist.model.Entiti;
import finassist.model.Ledger;
import finassist.model.Test;
import finassist.util.HibernateUtil;

/**
 * AJ Projects
 * LoginUserManagerDAO.java
 * Purpose: Login User Database.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class LedgerProcessManagerDAO extends HibernateUtil {

	/********** Add new Entity *************/
	public void add(Ledger ledger) {
		
		String query = "CREATE TABLE public.ledger_asasa_tbl (JOURNEL_ENTRY_ID character varying(40) NOT NULL,LEDGER_UID character varying(40)";
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(ledger);
		session.getTransaction().commit();
		
		Statement st = null;
        ResultSet rs = null;
        try{
            Class.forName("org.postgresql.Driver");
            System.out.println("connected");
            } catch (ClassNotFoundException cnfe){
              System.out.println("Could not find the JDBC driver!");
              System.exit(1);
            }
        Connection conn = null;
        try {
            //database location, database user, database password
            conn = DriverManager.getConnection
                           ("jdbc:postgresql:financial_assist_db","postgres", "admin");
            st = conn.createStatement();
            String qs = "CREATE TABLE IF NOT EXISTS public.Ledger_"+ledger.getLedgerName()+"_tbl(JOURNEL_ENTRY_ID character varying(40) NOT NULL,"+
  "LEDGER_UID character varying(40),DATE_OF_ENTRY character varying(40) NOT NULL,PARTICULARS character varying(40) NOT NULL,"+
  "AMOUNT character varying(40) NOT NULL,TRANS_TYPE character varying(40) NOT NULL,OPENING_BALANCE character varying(40) NOT NULL,"+
  "CONSTRAINT Ledger_"+ledger.getLedgerName()+"_tbl_pkey PRIMARY KEY (JOURNEL_ENTRY_ID),CONSTRAINT Ledger_"+ledger.getLedgerName()+"_tbl_ledger_uid_fkey FOREIGN KEY (LEDGER_UID)"+
  "REFERENCES fnas_ledger_master (LEDGER_UID) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION)";
      st.executeQuery(qs);

            System.out.println("connect");
             } catch (SQLException sqle) {
               System.out.println("Could not connect");
          }
		
		
	}
		
}
